package com.getpebble.android.framework.endpoint;

import android.content.Context;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundPhoneControlMessage;
import com.getpebble.android.framework.protocol.inbound.PblInboundPhoneControlMessage.PhoneControlAction;
import com.getpebble.android.framework.protocol.outbound.PblOutboundPhoneControlMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundPhoneControlMessage.PhoneControlCommand;
import com.getpebble.android.framework.util.PhoneUtil;
import com.google.common.collect.ImmutableSet;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.Set;

public class PhoneControlEndpoint extends RequestableEndpoint {
    public static final String TAG = PhoneControlEndpoint.class.getSimpleName();
    private Context mContext;
    private IEndpointMessageSender mMessageSender;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction = new int[EndpointAction.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundPhoneControlMessage$PhoneControlAction = new int[PhoneControlAction.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundPhoneControlMessage$PhoneControlAction[PhoneControlAction.HANGUP.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundPhoneControlMessage$PhoneControlAction[PhoneControlAction.UNKNOWN.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_PHONE_INCOMING_CALL_NOTIFICATION.ordinal()] = 1;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_PHONE_RING_NOTIFICATION.ordinal()] = 2;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_PHONE_START_NOTIFICATION.ordinal()] = 3;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_PHONE_END_NOTIFICATION.ordinal()] = 4;
            } catch (NoSuchFieldError e6) {
            }
        }
    }

    public PhoneControlEndpoint(Context context, IEndpointMessageSender messageSender) {
        if (messageSender == null) {
            throw new IllegalArgumentException("'messageSender' cannot be null!");
        } else if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else {
            this.mMessageSender = messageSender;
            this.mContext = context;
        }
    }

    boolean onRequest(EndpointRequest request, FrameworkState frameworkState) {
        byte[] cookie = request.getByteArray(EndpointArgumentKeys.PHONE_COOKIE);
        String name = request.getString(EndpointArgumentKeys.PHONE_NAME);
        String number = request.getString(EndpointArgumentKeys.PHONE_NUMBER);
        PblOutboundPhoneControlMessage message = null;
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[request.getAction().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                message = new PblOutboundPhoneControlMessage(PhoneControlCommand.INCOMING_CALL, cookie, number, name);
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                message = new PblOutboundPhoneControlMessage(PhoneControlCommand.RING, cookie);
                break;
            case ListInfo.INDETERMINATE /*3*/:
                message = new PblOutboundPhoneControlMessage(PhoneControlCommand.START, cookie);
                break;
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                message = new PblOutboundPhoneControlMessage(PhoneControlCommand.END, cookie);
                break;
        }
        if (message != null && this.mMessageSender.sendMessage(message)) {
            return true;
        }
        Trace.debug(TAG, "send failed");
        return false;
    }

    boolean onPrfRequest(EndpointRequest request, FrameworkState frameworkState) {
        return true;
    }

    Set<EndpointId> getSupportedEndpoints() {
        return ImmutableSet.of(EndpointId.PHONE_CONTROL);
    }

    boolean onReceive(ProtocolMessage message) {
        if (message == null) {
            Trace.error(TAG, "Received null message, dropping");
            return false;
        }
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundPhoneControlMessage$PhoneControlAction[new PblInboundPhoneControlMessage(message).getAction().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                endCall();
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                Trace.warning(TAG, "Unknown command");
                break;
        }
        return true;
    }

    protected void endCall() {
        PhoneUtil.endCall(this.mContext);
    }
}
